package com.ruoyi.web.controller.costofcontract;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProjectContract;
import com.ruoyi.service.IProjectContractService;
import com.ruoyi.system.domain.ProCreateBuilding;
import com.ruoyi.system.service.IProCreateBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 合同费用Controller
 *
 * @author ruoyi
 * @date 2020-06-23
 */
@Controller
@RequestMapping("/ruoyi/costofcontract")
public class CostOfContractController extends BaseController
{
    private String prefix = "ruoyi/costofcontract";
    @Autowired
    private IProCreateBuildingService proCreateBuildingService;
    @Autowired
    private IProjectContractService projectContractService;

    @RequiresPermissions("ruoyi:costofcontract:view")
    @GetMapping()
    public String costofcontract()
    {
        return prefix + "/costofcontract";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("ruoyi:costofcontract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProCreateBuilding proCreateBuilding)
    {
        startPage();
        List<ProCreateBuilding> list = proCreateBuildingService.selectProCreateBuildingList(proCreateBuilding);
        return getDataTable(list);
    }

    /**
     * 查询对应项目下的对应合同
     */
    @RequiresPermissions("ruoyi:costofcontract:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long sId) {
        startPage();
        List<ProjectContract> list = projectContractService.selectProjectContractlistss(sId);

        return getDataTable(list);
    }

    /**
     * 导出付款申请单合同费用
     */
    @Log(title = "项目合同数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("ruoyi:costofcontract:exports")
    @PostMapping("/exports")
    @ResponseBody
    public AjaxResult exports(ProjectContract projectContract)
    {
        List<ProjectContract> list = projectContractService.selectUserexport(projectContract);
        ExcelUtil<ProjectContract> util = new ExcelUtil<ProjectContract>(ProjectContract.class);
        return util.exportExcel(list, "项目合同数据");
    }

    /**
     * 修改项目合同
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap) {
        ProjectContract projectContract = projectContractService.selectProjectContractById(pId);
        mmap.put("projectContract", projectContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目合同
     */
    @RequiresPermissions("ruoyi:costofcontract:edit")
    @Log(title = "项目合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectContract projectContract) {
        ProjectContract projectContract2=projectContractService.selectProjectContractById(projectContract.getpId());

        //库里已付等于null 并且输入的已付<=库里总金额
        if(projectContract2.getpPaid()==null&&projectContract.getpPaid().compareTo(projectContract2.getpMoney())<1){
            //输入的已付带入
            projectContract.getpPaid();
            //存未付  库里总金额 - 输入的已付
            projectContract.setpNotpaid(projectContract2.getpMoney().subtract(projectContract.getpPaid()) );
            //如果库已付金额不等于null
        }else if(projectContract2.getpPaid()!=null){
            //如果库已付金额 + 已付金额  > 库总金额  则返回错误
            if(projectContract2.getpPaid().add(projectContract.getpPaid()).compareTo(projectContract2.getpMoney())==1){
                return toAjax(0);
            }else {
                BigDecimal sum=projectContract2.getpPaid().add(projectContract.getpPaid());
                projectContract.setpPaid(sum);
                //  计算未付金额(催款总金额-已付金额)
                if(projectContract2.getpNotpaid()!=null) {
                    BigDecimal unsum = projectContract2.getpMoney().subtract(sum);
                    projectContract.setpNotpaid(unsum);
                }else{
                    projectContract.setpNotpaid(projectContract2.getpMoney().subtract(projectContract.getpPaid()));
                }

            }
        }else{
            return toAjax(0);
        }

        return toAjax(projectContractService.updateProjectContract(projectContract));
    }
}
